﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace BadHabit.Factory.Views {

    /// <summary>
    /// Interaction logic for TeamEffort.xaml
    /// </summary>
    public partial class TeamEffortReport : BadHabit.WPFTools.ShapedWindow {


        private DateTime reportDate = DateTime.Today;

        /// <summary>
        /// Default Constructor.
        /// </summary>
        public TeamEffortReport() {
            InitializeComponent();
        }

        /// <summary>
        /// Loads the report when the window is loaded.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void window_Loaded(object sender, RoutedEventArgs e) {            
            LoadReport();
        }


        /// <summary>
        /// Loads the report.
        /// </summary>
        public void LoadReport() {

         
            string flowDocumentXAML;
            try {
                flowDocumentXAML = Client.Service.GenerateTeamReportXAML(reportDate.Month, reportDate.Year);
            } catch (Exception ex) {
                Views.Error.ShowError(ex, global::BadHabit.Factory.Windows.Properties.Resources.COULD_NOT_READ_REPORT);
                return;
            }

            System.IO.MemoryStream stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(flowDocumentXAML));

            FlowDocument document = (FlowDocument)XamlReader.Load(stream);
            flowDocumentViewer.Document = document;

        }

        /// <summary>
        /// Goes one month back on the report.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void previousMonthButton_Click(object sender, RoutedEventArgs e) {
            reportDate = reportDate.AddMonths(-1);
            LoadReport();
        }

        /// <summary>
        /// Goes one month forward on the report.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nextMonthButton_Click(object sender, RoutedEventArgs e) {
            if (reportDate.Date.Equals(DateTime.Today.Date))
                return;
            reportDate = reportDate.AddMonths(1);
            LoadReport();
        }
    }
}
